package ywq;

import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;
import java.awt.*;  
import java.awt.event.MouseAdapter;  
import java.awt.event.MouseEvent;  
import java.awt.image.BufferedImage;  
import java.io.File;  
import java.io.IOException;  
  
public class ImageCropper {  
  
    private JFrame frame;  
    private JPanel imagePanel;  
    private JButton insertImageButton;  
    private JButton clipButton;  
    private BufferedImage originalImage;  
    private Rectangle clipArea;  
  
    public ImageCropper() {  
        frame = new JFrame("Image Cropper");  
        imagePanel = new JPanel() {  
            @Override  
            protected void paintComponent(Graphics g) {  
                super.paintComponent(g);  
                if (originalImage != null) {  
                    g.drawImage(originalImage, 0, 0, this);  
                    if (clipArea != null) {  
                        g.setColor(Color.RED);  
                        g.drawRect(clipArea.x, clipArea.y, clipArea.width, clipArea.height);  
                    }  
                }  
            }  
        };  
        insertImageButton = new JButton("Insert Image");  
        clipButton = new JButton("Clip");  
  
        insertImageButton.addActionListener(e -> {  
            JFileChooser fileChooser = new JFileChooser();  
            FileNameExtensionFilter filter = new FileNameExtensionFilter("Image files", "jpg", "jpeg", "png", "gif");
            fileChooser.setFileFilter(filter);  
            int returnValue = fileChooser.showOpenDialog(frame);  
            if (returnValue == JFileChooser.APPROVE_OPTION) {  
                File selectedFile = fileChooser.getSelectedFile();  
                try {  
                    originalImage = ImageIO.read(selectedFile);  
                    imagePanel.setSize(originalImage.getWidth(), originalImage.getHeight());  
                    frame.pack();  
                } catch (IOException ex) {  
                    ex.printStackTrace();  
                }  
            }  
        });  
  
        clipButton.addActionListener(e -> {  
            if (clipArea != null) {  
                BufferedImage croppedImage = originalImage.getSubimage(clipArea.x, clipArea.y, clipArea.width, clipArea.height);  
                // TODO: 保存或显示裁剪后的图像 croppedImage  
                JOptionPane.showMessageDialog(frame, "Image cropped successfully!");  
                clipArea = null; // 重置裁剪区域  
                imagePanel.repaint(); // 重绘面板以移除裁剪框  
            } else {  
                JOptionPane.showMessageDialog(frame, "Please select a clipping area first.");  
            }  
        });  
  
        imagePanel.addMouseListener(new MouseAdapter() {  
            private Point startPoint;  
  
            @Override  
            public void mousePressed(MouseEvent e) {  
                startPoint = e.getPoint();  
                clipArea = new Rectangle(startPoint, new Dimension(0, 0));  
                imagePanel.repaint();  
            }  
  
            @Override  
            public void mouseReleased(MouseEvent e) {  
                Point endPoint = e.getPoint();  
                int width = Math.abs(endPoint.x - startPoint.x);  
                int height = Math.abs(endPoint.y - startPoint.y);  
                clipArea.setSize(width, height);  
                if (startPoint.x > endPoint.x) {  
                    clipArea.setLocation(endPoint.x, startPoint.y);  
                }  
                if (startPoint.y > endPoint.y) {  
                    clipArea.setLocation(startPoint.x, endPoint.y);  
                }  
                imagePanel.repaint();  
            }  
  
            @Override  
            public void mouseDragged(MouseEvent e) {  
                Point currentPoint = e.getPoint();  
                int width = Math.abs(currentPoint.x - startPoint.x);  
                int height = Math.abs(currentPoint.y - startPoint.y);  
                clipArea.setSize(width, height);  
                clipArea.setLocation(Math.min(currentPoint.x, startPoint.x), Math.min(currentPoint.y, startPoint.y));  
                imagePanel.repaint();  
            }  
        });  
  
        frame.setLayout(new BorderLayout());
        frame.add(imagePanel, BorderLayout.CENTER);
        JPanel controlsPanel = new JPanel();
        controlsPanel.add(insertImageButton);
        controlsPanel.add(clipButton);
        frame.add(controlsPanel, BorderLayout.SOUTH);

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(600, 400);
        frame.setLocationRelativeTo(null); // 居中显示
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> new ImageCropper());
    }
}